package com.retrofix;

import android.accounts.NetworkErrorException;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.retrofix.entity.BaseEntity;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import javax.net.ssl.SSLHandshakeException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * 基础Obsetver
 * @param <T>
 */
public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> {
    public static String TAG = BaseObserver.class.getSimpleName();

    protected Context mContext;

    public BaseObserver(Context cxt) {
        this.mContext = cxt;
    }

    public BaseObserver() {

    }

    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();

    }

    @Override
    public void onNext(BaseEntity<T> tBaseEntity) {
        onRequestEnd();
        if (tBaseEntity.isSuccess()) {
            try {
                onSuccess(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                onCodeError(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.w(TAG, "onError: " +  e.getMessage());
        onRequestEnd();
        try {
            if (e instanceof SocketTimeoutException) {//请求超时
                onFailure(new Throwable("请求超时"), true);
            } else if (e instanceof ConnectException) {//网络连接超时
                onFailure(new Throwable("网络连接超时"), true);
            } else if (e instanceof SSLHandshakeException) {//安全证书异常
                onFailure(new Throwable("安全证书异常"), true);
            } else if (e instanceof HttpException) {//请求的地址不存在
                int code = ((HttpException) e).code();
                if (code == 504) {
                    onFailure(new Throwable("网络异常，请检查您的网络状态"), true);
                } else if (code == 404) {
                    onFailure(new Throwable("请求的地址不存在"), true);
                } else {
                    onFailure(new Throwable("请求失败"), true);
                }
            } else if (e instanceof UnknownHostException) {//域名解析失败
                onFailure(new Throwable("域名解析失败"), true);
            } else {
                onFailure(e, false);
            }
            /*if (e instanceof ConnectException
                    || e instanceof TimeoutException
                    || e instanceof NetworkErrorException
                    || e instanceof UnknownHostException) {
                onFailure(e, true);
            } else {
                onFailure(e, false);
            }*/
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void onComplete() {

    }

    /**
     * 返回成功
     *
     * @param t
     * @throws Exception
     */
    protected abstract void onSuccess(BaseEntity<T> t) throws Exception;

    /**
     * 返回成功了,但是code错误
     * @param t
     * @throws Exception
     */
    protected void onCodeError(BaseEntity<T> t) throws Exception {

    }

    /**
     * 返回失败
     * @param e
     * @param isNetWorkError 是否是网络错误
     * @throws Exception
     */
    protected abstract void onFailure(Throwable e, boolean isNetWorkError) throws Exception;

    protected void onRequestStart() {

    }

    protected void onRequestEnd() {
        closeProgressDialog();
    }

    //显示loading
    public void showProgressDialog() {

    }

    //关闭loading
    public void closeProgressDialog() {

    }

}